<template>
  <div class="login-container">
    <div class="center">
      <div class="login-banner" />
      <div class="login-wrapper">
        <div class="qrcode" />
        <i class="code" @click="change" />
        <el-tabs v-model="activeName" stretch @tab-click="handleClick">
          <el-tab-pane label="账户登录" name="first">
            <el-form ref="loginForm" :model="loginForm" class="login-form" :rules="loginRules">
              <el-form-item prop="username">
                <i class="el-icon-user icon" />
                <el-input v-model="loginForm.username" placeholder="请输入用户名" @keyup.enter.native="handleLogin" />
              </el-form-item>
              <el-form-item prop="password">
                <i class="el-icon-lock icon" />
                <el-input v-model="loginForm.password" type="password" placeholder="请输入密码" @keyup.enter.native="handleLogin" />
              </el-form-item>
              <el-form-item style="margin-bottom: 0">
                <el-checkbox v-model="checked">记住密码</el-checkbox>
              </el-form-item>
              <el-form-item>
                <el-button :loading="loading" style="width: 100%; background-color:#1AA97B; color:aliceblue;" @click.native.prevent="handleLogin">登录</el-button>
              </el-form-item>
            </el-form>
            <div class="bottom">登录即代表您已阅读并同意<router-link to="/agreement"><span>《隐私协议和服务声明》</span></router-link></div>
          </el-tab-pane>
          <el-tab-pane label="手机登录" name="second">
            <el-form :data="form" class="login-form">
              <el-form-item>
                <i class="el-icon-user icon" />
                <el-input v-model="form.username" placeholder="请输入手机号" disabled />
              </el-form-item>
              <el-form-item>
                <i class="el-icon-lock icon" />
                <el-input v-model="form.verify" placeholder="请输入验证码" disabled>
                  <span slot="append" @click="send">{{ tip }}</span>
                </el-input>
              </el-form-item>
              <el-form-item style="height: 18px" />
              <el-form-item>
                <el-button style="width: 100%; background-color:#1AA97B; color:aliceblue;" @click="phoneSubmit">登录</el-button>
              </el-form-item>
            </el-form>
            <div class="bottom">登录即代表您已阅读并同意<router-link to="/agreement"><span>《隐私协议和服务声明》</span></router-link></div>
          </el-tab-pane>
        </el-tabs>
        <div v-show="show" class="show-qrcode">
          <div class="header" @click="show = false">返回账号登录</div>
          <div class="img">
            <!-- 二维码 -->
            <img src="../../assets/qrcode.png" alt="">
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { isCheck, setAccount, getAccount, rmAccount, setCheck, rmCheck } from '@/utils/account'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length === 0) {
        callback(new Error('请输入密码'))
      } else if (value.length < 8) {
        callback(new Error('密码不得少于8位'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: ''
      },
      form: {},
      loginRules: {
        username: [{ required: true, validator: validateUsername }],
        password: [{ required: true, validator: validatePassword }]
      },
      checked: false,
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      activeName: 'first',
      tip: '发送验证码',
      flag: true,
      show: false
    }
  },
  computed: {
    year() {
      return new Date().getFullYear()
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  mounted() {
    this.handleClick()
  },
  methods: {
    handleClick() {
      if (this.activeName === 'first' && this.show === false) {
        this.checked = isCheck()
        if (this.checked) {
          this.loginForm = Object.assign({}, getAccount())
        }
      }
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin(){
      if(this.loginForm.username === 'admin'&&this.loginForm.password === '12345678'){
        this.$router.push({ path: this.redirect || '/' })
      }
    },
    // handleLogin() {
    //   this.$refs.loginForm.validate(valid => {
    //     if (valid) {
    //       this.loading = true
    //       this.$store.dispatch('user/login', this.loginForm).then(() => {
    //         if (this.checked) {
    //           setAccount(this.loginForm)
    //           setCheck()
    //         } else {
    //           rmAccount()
    //           rmCheck()
    //         }
    //         setAccount(this.loginForm)
    //         this.$router.push({ path: this.redirect || '/' })
    //         this.loading = false
    //       }).catch(() => {
    //         this.loading = false
    //       })
    //     } else {
    //       return false
    //     }
    //   })
    // },
    change() {
      this.$notify({
        title: '警告',
        message: '此功能暂未开通',
        type: 'warning'
      })
      this.show = false
    },
    phoneSubmit() {
      this.$notify({
        title: '警告',
        message: '此功能暂未开通',
        type: 'warning'
      })
    },
    send() {
      if (this.flag) {
        this.$notify({
          title: '警告',
          message: '此功能暂未开通',
          type: 'warning'
        })
        return false
      }
      this.flag = true
      let time = 10
      let timer = null
      const _that = this
      timer = setInterval(function() {
        time -= 1
        _that.tip = `${time}秒重新获取`
        if (time === 0) {
          _that.flag = false
          _that.tip = '发送验证码'
          clearInterval(timer)
        }
      }, 1000)
    }
  }
}
</script>

<style lang="scss">
.login-container {
  height: 100%;
  min-height: 750px;
  position: relative;
  .center {
    width: 1000px;
    position: absolute;
    top: 250px;
    left: 50%;
    margin-left: -500px;
    .login-banner {
      // background: url('../../assets/banner.png') no-repeat;
      width: 500px;
      height: 400px;
      background-size: 100%;
      float: left;
    }
    .login-wrapper {
      float: right;
      width: 400px;
      background-color: #FFFFFF;
      border-radius: 10px;
      padding: 10px 40px 10px;
      overflow: hidden;
      box-shadow: 0 0 20px #E0E0E0;
      position: relative;

      .code {
        display: block;
        width: 30px;
        height: 30px;
        // 二维码
        background: url('../../assets/qrcode.png') no-repeat;
        background-size: 100% 100%;
        position: absolute;
        right: 5px;
        top: 5px;
        cursor: pointer;
      }

      .qrcode {
        width: 0;
        height: 0;
        border: 45px solid transparent;
        border-left-color: #ededed;
        position: absolute;
        transform: rotate(-45deg);
        right: -45px;
        top: -45px;
      }
    }
  }
  .el-tabs__item {
    color: #999999;
    font-size: 18px;
  }

  .el-tabs__item.is-active {
    color: #000000;
  }

  .el-tabs__active-bar {
    height: 3px;
  }

  .login-form {
  margin-top: 20px;

  .el-form-item {
    position: relative;

    .el-input__inner {
      padding-left: 40px;
    }

    .icon {
      position: absolute;
      z-index: 2;
      font-size: 20px;
      top: 8px;
      left: 10px;
      color: #666666;
    }
  }

  }

  .flex {
    display: flex;
    flex-direction: space-around
  }

  .el-input-group__append {
    width: 100px;
    text-align: center;
    cursor: pointer;
  }

  .show-qrcode {
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background: #FFFFFF;
    z-index: 3
  }

  .show-qrcode {
    .header {
      float: right;
      font-size: 14px;
      margin: 12px;
    }

    .img {
      width: 170px;
      height: 170px;
      position: absolute;
      left: 50%;
      margin-left: -85px;
      top: 50%;
      margin-top: -85px;
      img {
        width: 100%;
        height: 100%;
      }
    }
  }

  .bottom {
    font-size: 13px;
    text-align: center;
    margin-bottom: 20px;

    span {
      color: #066748;
    }
  }

  .page-bottom {
    width: 100%;
    background: #80979f;
    position: absolute;
    bottom: 0;
    color: #FFFFFF;
    text-align: center;
    padding: 15px 0;

    .page-content {
      min-width: 1000px;
    }
    h3 {
      font-weight: 500;
      font-size: 18px;
      line-height: 10px
    }
  }

  .police {
    display: inline-block;
    width: 20px;
    height: 20px;
    background: url("../../assets/icon.png")
  }
}
</style>
